<template>
  <div>
    <article class="innerPage">
      <header class="breadcrumb">
        <x-breadcrumb :path="$route.path" :title="$route.meta.breadcrumb" :startIndex="$route.meta.startIndex">
        </x-breadcrumb>
      </header>
      <div class="decorate"></div>

      <div class="graphical2" v-if="showSetting">
        <div class="title">数据配置</div>
        <div class="form">
          <el-input size="mini" v-model="title" placeholder="请输入表格标题" style="width: 420px"></el-input>
          <div class="row" style="margin-top:20px">
            <div class="closeItem" v-for="item in header">
              <el-input size="mini" class="input" v-model="item.value"
                placeholder="请输入表头名称"></el-input>
              <i class="el-icon-error close" @click="columnRemove(item.key)"></i>
            </div>
            <div class="plus" @click="columnPlus">+添加列</div>
          </div>
          <div class="row" v-for="(item) in data">
            <div class="closeItem" v-for="(h, index) in header">
              <el-input class="input" size="mini" placeholder="请输入数据" v-model="item[h.key]"></el-input>
              <i v-if="index == 0" class="el-icon-error close" @click="rowRemove(index)"></i>
            </div>
          </div>
          <div class="plus" style="margin-top:10px" @click="rowPlus">+添加行</div>
          <div class="action">
            <el-button type="primary" @click="previewData">数据预览</el-button>
            <div class="reset" @click="reset">重置数据</div>
          </div>
        </div>
      </div>
      <div class="decorate"></div>
      <div class="graphical2">
        <div class="top">
          <div class="title">数据统计</div>
          <div class="config" @click="showSetting=!showSetting">
            <i class="el-icon-s-tools"></i>
            {{showSetting?'隐藏':'显示' }}
          </div>
        </div>
        <el-table style="margin-top:10px" :data="showData" class="tableStyle" :header-cell-style="$headerCellStyle"
          :border="$isborder" size="mini" ref="elTable" stripe>
          <el-table-column :min-width="80" show-overflow-tooltip v-for="item in showHeaderData" :prop="item.key" :label="item.value"
            label-class-name="indexColumn" align="center" >
          </el-table-column>
        </el-table>
        <div class="action" v-if="showSetting">
          <el-button type="primary" @click="save">保存数据</el-button>
        </div>
      </div>
    </article>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showSetting:true,
      title: "",
      headerIndex: 2,
      // 初始数据
      resetHeader: [
        { key: "value0", value: "" },
        { key: "value1", value: "" },
      ],
      resetData: [
        {
          value0: "",
          value1: "",
        },
      ],
      // 上层编辑显示数据
      header: [],
      data: [],
      // 表格显示数据
      showHeaderData: [],
      showData: [],
    };
  },
  methods: {
    previewData() {
      this.showHeaderData = JSON.parse(JSON.stringify(this.header))
      const data = this.data.filter(item => {
        let count = 0
        this.header.forEach(i => {
          if (!item[i.key]) {
            count++
          }
        })
        return count < this.header.length
      })
      this.showData = JSON.parse(JSON.stringify(data))
    },
    columnPlus() {
      this.header.push({
        key: `value${this.headerIndex++}`,
        value: "",
      });
    },
    rowRemove(index) {
      if (this.data.length == 1) {
        this.$message.warning('至少保留一行数据')
        return
      }
      this.data.splice(index, 1)

    },
    // 删除列
    columnRemove(key) {
      if (this.header.length == 1) {
        this.$message.warning('至少保留一列')
        return
      }

      // 表头删除
      this.header = this.header.filter(item => item.key != key)

      // 数据删除
      for (let index = 0; index < this.data.length; index++) {
        delete this.data[index][key];
      }
    },
    rowPlus() {
      let _data = {}
      this.header.forEach(item => {
        _data[item.key] = ''
      })

      this.data.push(_data)
    },
    reset() {
      this.title = ''
      this.headerIndex = 2
      this.header = JSON.parse(JSON.stringify(this.resetHeader))
      this.data = JSON.parse(JSON.stringify(this.resetData))
      this.showData = JSON.parse(JSON.stringify([]))
      this.showHeaderData = JSON.parse(JSON.stringify([]))
    },
    save() {
      if (!this.title) {
        this.$message.warning('请输入标题')
        return
      }

      if (!this.showData.length) {
        this.$message.warning('数据不能为空')
        return
      }
      let headerFlag = true
      this.showHeaderData.forEach(item => {
        if (item.value == '') {
          headerFlag = false
        }
      })
      if (!headerFlag) {
        this.$message.warning('表头不能为空')
        return
      }



      const content = JSON.stringify({
        header: this.showHeaderData,
        data: this.showData
      })
      this.request({
        url: 'admin/externalDataRecord/add', method: 'POST', data: {
          title: this.title,
          content,
        }
      }).then(res => {
        this.reset()
        this.$message.success(res.msg)
      }).catch(e => {
        this.errorHandler(e);
      })

    }
  },
  mounted() {
    this.reset()
  }
};
</script>

<style lang="scss" scoped>
@import url("./style.scss");

.form {
  margin-top: 20px;

  .row {
    margin-top: 10px;
    display: flex;
    flex-wrap: nowrap;
    gap: 10px;

    .closeItem {
      position: relative;

      .close {
        position: absolute;
        right: 3px;
        top: 50%;
        transform: translateY(-50%);
        color: #dfdfdf;
        cursor: pointer;
      }

      .close:hover {
        color: #F56C6C;
      }

    }

    .input {
      width: 140px;
      flex-shrink: 0;
    }


  }

  .plus {
    flex-shrink: 0;
    width: 120px;
    background: rgba($color: #00a9b0, $alpha: 0.1);
    border: 1px dashed $themeColor;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    cursor: pointer;

    font-size: 14px;
    font-weight: 400;
    text-align: left;
    color: #00a9b0;
    height: 28px;
  }
}

.action {
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 40px;

  .reset {
    width: 120px;
    height: 32px;
    background: #f2f2f2;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;

    font-size: 14px;
    font-family: MicrosoftYaHei;
    font-weight: normal;
    text-align: center;
    color: #666666;
  }
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  color: #00a9b0;

  .config {
    opacity: 0;
    transition: all .2s;
  }
}

.config:hover {
  opacity: 1;
}
</style>
